//
//  CZVistorView.swift
//  微博08
//
//  Created by apple on 16/2/26.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

// 1.定义协议
protocol CZVistorViewDelegate: NSObjectProtocol {
    func vistorViewRegisterButtonClick()
    func vistorViewLoginButtonClick()
}

class CZVistorView: UIView {
    
    // 2.定义代理属性
    weak var vistorViewDelegate: CZVistorViewDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        
        prepareUI()
    }

    // 从xib/storyboard里面加载会走这个方法
    // 当子类实现init(frame: CGRect)就不会继承父类的构造函数init?(coder aDecoder: NSCoder),就不能从xib/storyboard里面加载
    // 只有同时实现init(frame: CGRect)和init?(coder aDecoder: NSCoder)才能保证view即可以使用代码创建,也可以使用xib/storyboar来创建
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        // 做自己的事情
        super.init(coder: aDecoder)
    }
    
    func prepareUI() {
        // 1.将懒加载的控件添加到父控件里面
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2.约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        
        // 转轮 CenterX
        // 约束都添加到父控件上面
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // 转轮 CenterY
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        // 房子
        // CenterX和转轮CenterX重合
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // CenterY和转轮CenterY重合
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // messageLabel
        // 宽度 240
        // 注意: 当toItem为nil时,attribute必须填:NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        
        // CenterX和转轮CenterX重合
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        
        // Top和转轮Bottom间距16
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        
        // 注册按钮
        // 左边和消息的左边重合
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        // Top和消息Bottom间距16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 宽度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        
        // 高度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        // 登录按钮
        // 右边和消息的右边重合
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        // Top和注册按钮的Top对齐
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        
        // 宽度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        
        // 高度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        
        // 遮盖
        // 填充父控件
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    
    // 只有消息发现我需要设置访客视图
    /**
     设置访客视图
     
     - parameter imageName: 图片的名称
     - parameter message:   消息
     */
    func setupVisitorView(imageName: String, message: String) {
        // 隐藏房子
        homeView.hidden = true
        
        // 设置图片
        iconView.image = UIImage(named: imageName)
        
        // 设置message
        messageLabel.text = message
        
        // 隐藏遮盖
//        coverView.hidden = true
        // 将子view移动到最底层
        sendSubviewToBack(coverView)
        
        // 将子view移动到最上层
//        bringSubviewToFront(coverView)
    }
    
    /// 转轮旋转
    func iconViewRotation() {
        // 创建核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        
        // 动画完成的时候不要移除,不然切换到其它界面在切换回来就不会动画了
        anim.removedOnCompletion = false
        
        // 将核心动画添加到view上面
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    // MARK: - 按钮点击事件
    func registerButtonClick() {
        // 3.调用代理方法
        vistorViewDelegate?.vistorViewRegisterButtonClick()
    }
    
    func loginButtonClick() {
        // 3.调用代理方法
        vistorViewDelegate?.vistorViewLoginButtonClick()
    }
    
    // MARK: - 懒加载
    // 小房子
    lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    // 转轮
    lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    // 消息Label
    lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        // 设置
        label.text = "关注一些人,看看有上面惊喜"
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        
        return label
    }()
    
    // 注册按钮
    lazy var registerButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        
        // 添加点击事件
        button.addTarget(self, action: "registerButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    // 登录按钮
    lazy var loginButton: UIButton = {
        let button = UIButton()
        
        // 设置
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        
        // 添加点击事件
        button.addTarget(self, action: "loginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    // 遮盖
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}
